package com.letwal.cmcvnd.mywallet.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.coin.RevDataGetCoins
import com.letwal.cmcvnd.extend.isLink
import com.letwal.cmcvnd.extend.toEllipsisWalletAddress

class MyWalletAdapter(private val dataList: List<RevDataGetCoins>) :
    RecyclerView.Adapter<MyWalletAdapter.ViewHolder>() {

    companion object {
        const val ACTION_CLICK = 0
        val ACTION_MORE = 1

        val BG_COLORS = mutableListOf(
            R.drawable.my_wallet_card1,
            R.drawable.my_wallet_card2,
            R.drawable.my_wallet_card3,
            R.drawable.my_wallet_card4
        )
    }

    //点击事件
    private var mItemClickListener: (Int, RevDataGetCoins) -> Unit = { action, item -> }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var rlAdapterMyWallet: RelativeLayout = view.findViewById(R.id.rlAdapterMyWallet)
        var imageAdapterMyWallet: ImageView =
            view.findViewById(R.id.imageAdapterMyWallet)
        var imageAdapterMyWalletMore: ImageView =
            view.findViewById(R.id.imageAdapterMyWalletMore)
        var textAdapterMyWalletName: TextView =
            view.findViewById(R.id.textAdapterMyWalletName)
        var textAdapterMyWalletAddress: TextView =
            view.findViewById(R.id.textAdapterMyWalletAddress)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_my_wallet_v2, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevDataGetCoins = dataList[position]
        holder.textAdapterMyWalletName.text = data.name
        holder.textAdapterMyWalletAddress.text = data.address.toEllipsisWalletAddress()

        //随机颜色
//        val modIndex = position % BG_COLORS.size
//        holder.rlAdapterMyWallet.setBackgroundResource(BG_COLORS[modIndex])

        holder.rlAdapterMyWallet.setOnClickListener {
            mItemClickListener.invoke(ACTION_CLICK, data)
        }

        holder.imageAdapterMyWalletMore.setOnClickListener {
            mItemClickListener.invoke(ACTION_MORE, data)
        }

        if (data.logo.toString().isLink()) {
            Glide.with(holder.imageAdapterMyWallet.context).load(data.logo)
                .into(holder.imageAdapterMyWallet)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    /** 设置点击事件 */
    fun setItemClickListener(listener: (Int, RevDataGetCoins) -> Unit) {
        mItemClickListener = listener
    }
}